package com.huixh.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huixh.train.business.domain.Train;
import com.huixh.train.business.domain.TrainExample;
import com.huixh.train.business.mapper.TrainMapper;
import com.huixh.train.business.req.TrainQueryReq;
import com.huixh.train.business.req.TrainSaveReq;
import com.huixh.train.business.resp.TrainQueryResp;
import com.huixh.train.business.service.TrainService;
import com.huixh.train.common.resp.PageResult;
import com.huixh.train.common.resp.Result;
import com.huixh.train.utils.random.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huixh
 */
@Service
public class TrainServiceImpl implements TrainService {

    private final Logger logger = LoggerFactory.getLogger(TrainServiceImpl.class);
    @Resource
    private TrainMapper trainMapper;

    @Resource
    private SnowUtil snowUtil;

    @Override
    public void save(TrainSaveReq trainSaveReq) {
        DateTime now = DateTime.now();
        Train train = BeanUtil.copyProperties(trainSaveReq, Train.class);
        if (ObjectUtil.isNull(train.getId())) {
            train.setId(snowUtil.getSnowflakeToLong());
            train.setCreateTime(now);
            train.setUpdateTime(now);
            trainMapper.insert(train);
        } else {
            train.setUpdateTime(now);
            trainMapper.updateByPrimaryKey(train);
        }
    }

    @Override
    public PageResult<List<TrainQueryResp>> queryList(TrainQueryReq trainQueryReq) {
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("id desc");
        logger.info("查询页码：{}", trainQueryReq.getPageNum());
        logger.info("每页条数：{}", trainQueryReq.getPageSize());
        PageHelper.startPage(trainQueryReq.getPageNum(), trainQueryReq.getPageSize());
        List<Train> trainList = trainMapper.selectByExample(trainExample);

        PageInfo<Train> pageInfo = new PageInfo<>(trainList);
        logger.info("总行数：{}", pageInfo.getTotal());
        logger.info("总页数：{}", pageInfo.getPages());

        List<TrainQueryResp> list = BeanUtil.copyToList(trainList, TrainQueryResp.class);
        return PageResult.success(pageInfo.getTotal(), list);
    }

    @Override
    public void delete(Long id) {
        trainMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询所有车次
     *
     * @return
     */
    @Override
    public Result<List<TrainQueryResp>> queryList() {
        List<Train> trains = selectAll();
        List<TrainQueryResp> respList = BeanUtil.copyToList(trains, TrainQueryResp.class);
        return Result.success(respList);
    }

    private Train selectByUnique(String trainCode) {
        TrainExample trainExample = new TrainExample();
        trainExample.createCriteria()
                .andCodeEqualTo(trainCode);
        List<Train> trainList = trainMapper.selectByExample(trainExample);

        if (CollUtil.isEmpty(trainList)) {
            return null;
        } else {
            return trainList.get(0);
        }
    }
    @Override
    public List<Train> selectAll(){
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");
        return trainMapper.selectByExample(trainExample);
    }
}